/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package JFreeChartExtensions.chartextensions.inset;

import JFreeChartExtensions.chartextensions.utils.Inset;
import JFreeChartExtensions.chartextensions.utils.InsetChartPanel;
import JFreeChartExtensions.ExtendableChartPanel;
import JFreeChartExtensions.JChartMenu.JChartMenu;
import JFreeChartExtensions.chartextensions.AbstractStorableChartPanelExtension;
import JFreeChartExtensions.JChartMenu.ChartMenuListeners.InsetSettingsListener;
import JFreeChartExtensions.containers.InternalFrameWithChart.InternalFrameWithChart;
import JFreeChartExtensions.containers.InternalFrameWithChart.InternalFrameWithChartException;
import JFreeChartExtensions.exceptions.ExtensionLauncherException;
import JFreeChartExtensions.storable.StorableExtenstionObject;
import java.awt.Rectangle;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JCheckBoxMenuItem;
import org.jfree.chart.ChartPanel;

/**
 *
 * @author Nobody
 */
public class InsetExtension extends AbstractStorableChartPanelExtension {

    private Inset inset;
    private InsetChartPanel chartPanel1;
    private JChartMenu insetSettingsMenuItem = new JChartMenu("Inset Settings...");
    private JCheckBoxMenuItem putInsetMenuItem = new JCheckBoxMenuItem("Put inset on chart");

    @Override
    public void loadStoredExtension(StorableExtenstionObject extenstionObject) {
        // inset.getSmallPanel().getDataSeriesCollections().putAll(chartPanel.getDataSeriesCollections());
    }

    @Override
    public StorableExtenstionObject storeExtension() {
        return new StorableExtenstionObject(null, name);
    }

    public Inset getInset() {
        return inset;
    }

    public InsetChartPanel getInsetChartPanel() {
        return chartPanel1;
    }

    public InsetExtension(ExtendableChartPanel panel) {
        super(panel);
        name = "InsetExtension";
    }

    private void addInsetToDesktopPane(Inset inset) {
        this.chartPanel.getParentContainer().addInternalFrame(inset);
    }

    @Override
    public void applyExtension(final ChartPanel chartPanel) throws ExtensionLauncherException {
        this.chartPanel = (ExtendableChartPanel) chartPanel;

        try {
            inset = new Inset(this.chartPanel.getParentContainer(), this.chartPanel.getChart(), this.chartPanel.getDatasetCollection());
            if (!(this.chartPanel.getParentContainer() instanceof InternalFrameWithChart)) {
                throw new ExtensionLauncherException("Inset can be applied only to internal frame");
            }
            InternalFrameWithChart parentFrame = (InternalFrameWithChart) this.chartPanel.getParentContainer();
            parentFrame.addInternalFrameListener(inset);
            chartPanel1 = new InsetChartPanel(chartPanel.getChart());
        } catch (CloneNotSupportedException ex) {
            throw new ExtensionLauncherException("exception during applying inset exception", ex);
        } catch (InternalFrameWithChartException ex) {
            throw new ExtensionLauncherException("exception during applying inset exception", ex);
        }
        addInsetToDesktopPane(inset);
        chartExtensionManager = new ChartInsetManager(chartPanel, inset, chartPanel1);
        chartExtensionManager.register(chartPanel);
        ChartInsetManager chartInsetManager = (ChartInsetManager) chartExtensionManager;

        // for (Entry<Integer,XYReggresionSeriesCollection> entry : this.chartPanel.getDatasetCollection().getDataSeriesCollections().entrySet()) {
        this.chartPanel.getDatasetCollection().addSeriesAddedListener(chartInsetManager);
        //     entry.getValue().addSeriesAddedListener(chartInsetManager);
        // }
        insetSettingsMenuItem.addListener(new InsetSettingsListener(chartInsetManager, (ExtendableChartPanel) chartPanel));
        chartPanel.getPopupMenu().add(insetSettingsMenuItem);
        chartPanel1.setBounds(inset.getBounds());
        chartPanel.add(chartPanel1);
        chartPanel1.setVisible(false);
        chartPanel.addComponentListener(chartPanel1);



        putInsetMenuItem.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                if (putInsetMenuItem.isSelected()) {
                    try {
                        chartPanel.remove(chartPanel1);
                        Rectangle rect = (Rectangle) chartPanel1.getBounds().clone();
                        chartPanel1 = new InsetChartPanel(inset.getSmallChart());
                        ((ChartInsetManager) chartExtensionManager).setInsetChartPanel(chartPanel1);
                        chartPanel1.setBounds(rect);
                        chartPanel1.setVisible(true);
                        chartPanel1.repaint();
                        chartPanel.addComponentListener(chartPanel1);
                        chartPanel.repaint();
                        chartPanel.add(chartPanel1);
                    } catch (CloneNotSupportedException ex) {
                        Logger.getLogger(InsetExtension.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else {
                    chartPanel1.setVisible(false);
                }
                chartPanel.repaint();
            }
        });
        chartPanel.getPopupMenu().add(putInsetMenuItem);

    }

    @Override
    public void removeExtension() {
        chartPanel.getParentContainer().removeInternalFrame(inset);
       chartExtensionManager.unregister();
        chartPanel.getPopupMenu().remove(insetSettingsMenuItem);
        chartPanel.remove(chartPanel1);
        chartPanel.removeComponentListener(chartPanel1);
        chartPanel.getPopupMenu().remove(putInsetMenuItem);


    }
}
